package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.NoteComment;
import cn.wolfcode.wolf2w.business.mapper.NoteCommentMapper;
import cn.wolfcode.wolf2w.business.query.NoteCommentQuery;
import cn.wolfcode.wolf2w.business.service.INoteCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 游记评论Service业务层处理
 * 
 * @author zty
 * @date 2025-09-16
 */
@Service
@Transactional
public class NoteCommentServiceImpl extends ServiceImpl<NoteCommentMapper,NoteComment> implements INoteCommentService {
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Override
    public IPage<NoteComment> queryPage(NoteCommentQuery qo) {
        IPage<NoteComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());

        IPage<NoteComment> page1 = lambdaQuery().eq(NoteComment::getNoteId, qo.getNoteId()).page(page);
        for (NoteComment comment : page1.getRecords()) {
            comment.setUser(remoteUserInfoService.getOne(comment.getUserId(), SecurityConstants.INNER).getData());
            if (comment.getRefId() != null) {
                // 加载被回复的评论
                NoteComment refComment = lambdaQuery().eq(NoteComment::getId, comment.getRefId()).one();
                // 同时加载被回复评论的用户信息
                if (refComment != null) {
                    refComment.setUser(remoteUserInfoService.getOne(refComment.getUserId(), SecurityConstants.INNER).getData());
                }
                comment.setRefComment(refComment);
            }

        }
        return page1;

    }

    @Override
    public void add(NoteComment noteComment) {
        NoteComment comment = new NoteComment();
        comment.setNoteId(noteComment.getNoteId());
        comment.setNoteTitle(noteComment.getNoteTitle());
        comment.setUserId(SecurityContextHolder.getUserId());
        comment.setContent(noteComment.getContent());

        if (noteComment.getRefId()!=null){
            comment.setRefId(noteComment.getRefId());
        }
        /*默认为null*/
        //comment.setType();
        comment.setStatus("0");
        /*后续填写*/

        comment.setCreateTime(new Date());
        baseMapper.insert(comment);

    }


}
